﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class PersonnelGroupPermissionViewModel : NotificationObject
    {
        private PersonnelGroupPermission gWindow;
        private PersonnelGroupViewModel gPersonnelGroupViewModel;
        private CPersonnelGroup gPersonnelGroup;

        public PersonnelGroupPermissionViewModel(PersonnelGroupPermission pWindow, PersonnelGroupViewModel pPersonnelGroupViewModel, CPersonnelGroup pPersonnelGroup)
        {
            gWindow = pWindow;
            gPersonnelGroupViewModel = pPersonnelGroupViewModel;
            gPersonnelGroup = pPersonnelGroup;

            Initialize();
        }

        public void Initialize()
        {
            Name = gPersonnelGroup.Name;

            HttpResponse resPermission = API.SelectPermission();
            HttpResponse resPersonnelGroupPermission = API.SelectPersonnelGroupPermission(gPersonnelGroup.UID);

            if (resPermission.S == HttpResponse.Success && resPersonnelGroupPermission.S == HttpResponse.Success)
            {
                List<CPersonnelGroupPermission> personnelGroupPermissionList = new List<CPersonnelGroupPermission>();
                if (resPersonnelGroupPermission.D != "")
                {
                    personnelGroupPermissionList = JSON.parse<List<CPersonnelGroupPermission>>(resPersonnelGroupPermission.D);
                }

                List<CPermission> permissionList = new List<CPermission>();
                if (resPermission.D != "")
                {
                    permissionList = JSON.parse<List<CPermission>>(resPermission.D);
                }

                for (int i = 0; i < permissionList.Count; i++)
                {
                    CheckBox checkBox = new CheckBox();
                    checkBox.Margin = new System.Windows.Thickness(5);
                    checkBox.Content = permissionList[i].Name;
                    checkBox.Uid = permissionList[i].UID;
                    gWindow.PersonnelGroupPermissions.Children.Add(checkBox);

                    foreach (var item in personnelGroupPermissionList)
                    {
                        if (item.PermissionUID == permissionList[i].UID)
                        {
                            checkBox.IsChecked = true;
                            break;
                        }
                    }
                }
            }
            else
            {
                gWindow.Close();
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            API.UpdatePersonnelGroup(new CPersonnelGroup(gPersonnelGroup.UID, Name), gPersonnelGroup.UID);

            List<CPersonnelGroupPermission> list = new List<CPersonnelGroupPermission>();
            foreach(var control in gWindow.PersonnelGroupPermissions.Children)
            {
                if(control is CheckBox)
                {
                    if(((CheckBox)control).IsChecked == true)
                    {
                        list.Add(new CPersonnelGroupPermission(gPersonnelGroup.UID, ((CheckBox)control).Uid));
                    }
                }
            }
            API.DeletePersonnelGroupPermission(gPersonnelGroup.UID);
            API.InsertPersonnelGroupPermission(list);

            gPersonnelGroupViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }
    }
}
